layui.use(['table', 'admin','laydate', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
        elem: '#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem: '#endTime'
        ,value:new Date()
    });


    /**
     * 拣货单拉取管理
     */
    var FiOrderAsyc = {
        tableId: "fiOrderAsycTable"
    };

    /**
     * 初始化表格的列
     */
    FiOrderAsyc.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'orderNumber', title: '订单号',width:160},
            {field: 'setMentiontime', sort: true, title: '提货时间',width:100},
            {field: 'getUnit', sort: true, title: '收货门店',width:180},
            {field: 'totalvolume', sort: true, title: '体积',width:100},
            {field: 'dispatchNumber', sort: true, title: '运输单号',width:180},
            {field: 'sysTotalvolume', sort: true, title: '系统体积',width:100},
            {field: 'sysToovolume', sort: true, title: '系统已配体积',width:140},
            {field: 'operateName', sort: true, title: '所属项目',width:180},
            {field: 'createTime', sort: true, title: '拉取时间',width:180},
            {field: 'createName', sort: true, title: '拉取用户',width:100},
            {align: 'center', toolbar: '#tableBar', title: '操作',width:100}
        ]];
    };

    /**
     * 点击查询按钮
     */
    FiOrderAsyc.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['dispatchNumber'] = $("#dispatchNumber").val();
        queryData['orderNumber'] = $("#orderNumber").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        table.reload(FiOrderAsyc.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    FiOrderAsyc.exportExcel = function () {
        var checkRows = table.checkStatus(FiOrderAsyc.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    FiOrderAsyc.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/fiOrderAsyc/edit?orderNumber=' + data.orderNumber;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    FiOrderAsyc.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/fiOrderAsyc/replaceOne", function (data) {
                Feng.success("替换成功!");
                table.reload(FiOrderAsyc.tableId);
            }, function (data) {
                Feng.error("替换失败!" + data.responseJSON.message + "!");
            });
            ajax.set("orderNumber", data.orderNumber);
            ajax.start();
        };
        Feng.confirm("是否替换?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + FiOrderAsyc.tableId,
        url: Feng.ctxPath + '/fiOrderAsyc/list',
        page: true,
        limit: 60,
        limits:[60,120,240,360,480,600,2000,5000],
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: FiOrderAsyc.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        FiOrderAsyc.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        FiOrderAsyc.exportExcel();
    });

    //拉取新零帮拣货任务
    $('#pullxlbStoragetask').click(function () {
        submit = 2
        layer.open({
            type: 2,
            title: '拉取新零帮拣货任务',
            area: ['850px', '600px'],
            anim: 5,
            content: Feng.ctxPath + '/fiOrderAsyc/topullxlbStoragetask',
            end: function () {
                if(submit == 1){
                    table.reload(FiOrderAsyc.tableId);
                }
            }
        });
    });

    // 工具条点击事件
    table.on('tool(' + FiOrderAsyc.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            FiOrderAsyc.openEditDlg(data);
        } else if (layEvent === 'delete') {
            FiOrderAsyc.onDeleteItem(data);
        }
    });
});
